<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php 

class CMiniyunStatForm extends CFormModel
{
	public $model;
	public $statData; 	public $startDate;	public $endDate;  	public $mixDate;  	public $maxDate;  	public $hasData;  	public function rules(){
		return array(
		array('startDate,endDate', 'length', 'max'=>255),
		);
	}
	public function initData(){
		$this->_initDate();				if(!isset($this->startDate)||strtotime($this->startDate)<strtotime($this->mixDate)){
			$this->startDate = $this->mixDate;
		}
				if(!isset($this->endDate)||strtotime($this->endDate)>strtotime($this->maxDate)){
			$this->endDate = $this->maxDate;
		}
		$this->statData = array();
		foreach($this->chartMethods() as $index=>$method){
			array_push($this->statData,$this->{$method}());
		}
	}
	
	protected  function chartMethods(){
		return array();
	}
	
	protected   function getInitDataSql()
	{
		return "SELECT DATE(max(created_at)) as endDate,DATE(min(created_at)) as startDate FROM `".$this->model->tableName();
	}
	
	private function _initDate(){
		$sql = $this->getInitDataSql();
		$dbCommand = Yii::app()->db->createCommand($sql);
		$data =  $dbCommand->queryAll();
		$this->hasData = true;
		$this->mixDate   = $data[0]["startDate"];
		$this->maxDate   = $data[0]["endDate"];
		if(!isset($this->mixDate)||!isset($this->maxDate)){
			$this->hasData = false;
		}
	}


	
	protected  function getSampleHistogramData(){
		$space= 10;		$s = strtotime($this->startDate);
		$e = strtotime($this->endDate);
		$diff = (int)($e-$s)/(24*3600);
		if($diff>$space){
			$spaceValue = (int)$diff/$space;		}else{
			$spaceValue = 1;		}
		$x = array();
		for($item=$s; $item<=$e; $item+=$spaceValue*24*3600){
			array_push($x,date("Y-m-d",$item));
		}
		$data = array();		foreach($x as $index=>$currentDate){
			$data[$currentDate]=(int)($this->model->count("created_at<='".$currentDate." 23:59:59'"));
		}
		return $data;

	}
}